#include "IndexLoader.h"
#include "MessageHandler.h"

#include <QDebug>
#include <QDomDocument>

IndexLoader::IndexLoader(const QString FileName):
    m_fileName(FileName)
{
}

bool IndexLoader::loadFile()
{
    bool loadCondition = false;

    QFile file(m_fileName);

    if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
          if(validate())
          {

              QDomDocument document("index");
              if(document.setContent(&file))
              {
                    loadCondition = true;
                  file.close();

                   QDomElement docElem = document.documentElement();

                  QDomNode root = docElem.firstChild();

                  while(!root.isNull())
                  {
                  QDomElement element  = root.toElement();

                  if(!element.isNull())
                  {
                      QString name;
                      QString kind = element.attribute("kind");
                      QString refid = element.attribute("refid");

                      QDomNodeList list = element.childNodes();

                      for(int i = 0; i < list.size() && name.isEmpty(); i++)
                      {
                          if(list.at(i).toElement().tagName() == "name")
                          {
                              name = list.at(i).toElement().text();
                          }
                      }
                      if(kind == "file")
                      {
                        m_fileMap.insert(refid,name);
                      }
                      else if(kind == "class")
                      {
                        m_classMap.insert(refid,name);
                      }
                  }
                  root = root.nextSibling();
              }
             }

          //load the file into qDom tree as it should be quite small
          }
    }

    return loadCondition;
}

void IndexLoader::getFileNames(QList<RefItem> &fileNames)
{
    fileNames.clear();

    QMapIterator<QString,QString> itor(m_fileMap);

    while(itor.hasNext())
    {
        itor.next();

        RefItem item;
        item.name = itor.value();
        item.refId = itor.key();
        fileNames.push_back(item);
    }
}

void IndexLoader::getClassNames(QList<RefItem> &classNames)
{
    classNames.clear();

    QMapIterator<QString,QString> itor(m_classMap);

    while(itor.hasNext())
    {
        itor.next();

        RefItem item;
        item.name = itor.value();
        item.refId = itor.key();
        classNames.push_back(item);
    }
}

bool IndexLoader::validFileRef(const QString refid) const
{
    bool found = false;

    QMap<QString,QString>::const_iterator itor;

    itor  = m_fileMap.find(refid);

    if(itor != m_fileMap.end())
    {
        found = true;
    }

    return found;
}

bool IndexLoader::validClassRef(const QString refid) const
{
    bool found = false;

    QMap<QString,QString>::const_iterator itor;

    itor  = m_classMap.find(refid);

    if(itor != m_classMap.end())
    {
        found = true;
    }

    return found;
}

bool IndexLoader::validate()
{
    bool valid = false;
    MessageHandler handler;

    QXmlSchema schema;
    schema.setMessageHandler(&handler);
    schema.load(QUrl::fromLocalFile(":/scheema/index.xsd"));

    if(schema.isValid())
    {
        QXmlSchemaValidator validator(schema);

        if(validator.validate(QUrl::fromLocalFile(m_fileName)))
        {
                valid = true;
        }
        else
        {
            qDebug() << "Error in index file format";
        }

    }
    else
    {
        qDebug() << "schema did not load";
    }

    return valid;
}
